#!/usr/bin/env python
# -*- mode: python; coding: utf-8; -*-

import unittest
import xml.etree.ElementTree as etree

from api4 import User4k, Envelope, date4

USER_XML = """
        <user>
            <country code="ru">Россия</country>
            <currency code="RUB" id="ru">руб</currency>
            <firstDayOfWeek>2</firstDayOfWeek>
            <timeZone>Europe/Moscow</timeZone>
            <disableExtendedSyntax>false</disableExtendedSyntax>
            <persons>
                <person id="15009487" name="Пользователь"/>
            </persons>
        </user> """

ENVELOPE_XML = """
        <envelope begin="2009-11-16" size="2346.0">
            <person id="15009487" name="Пользователь">
                <dailyExpense date="2009-11-16">
                    <expression>300 корректировка</expression>
                    <sum>300.0</sum>
                </dailyExpense>
                <dailyExpense date="2009-11-17">
                    <expression>83 билет, 120 обед, 20 орбит, 100 корректировка,</expression>
                    <sum>383.0</sum>
                </dailyExpense>
                <dailyExpense date="2009-11-18">
                    <expression>300 корректировка</expression>
                    <sum>300.0</sum>
                </dailyExpense>
                <dailyExpense date="2009-11-19"><expression></expression><sum>320.0</sum></dailyExpense><dailyExpense date="2009-11-20"><expression>500 продукты</expression><sum>500.0</sum></dailyExpense><dailyExpense date="2009-11-21"><expression>200 торт, 70 корректировка</expression><sum>270.0</sum></dailyExpense><dailyExpense date="2009-11-22"><expression>200 корректировка</expression><sum>200.0</sum></dailyExpense>
            </person>
        </envelope>
"""

class Envelope4k(object):

    def __init__(self):
        self._factory = {
            'currency': self.__handle_currency,
            'country': self.__handle_country,
            'firstDayOfWeek': self.__handle_firstday,
            'persons': self.__handle_persons,
        }

    def __handle_persons(self, element):
        for person in element:
            pid = person.attrib['id']
            name = person.attrib['name']
            self._user.append_person(pid, name)

    def __handle_firstday(self, element):
        day = int(element.text)
        self._user.firstday = day

    def __handle_country(self, element):
        code = element.attrib['code']
        self._user.set_country(code, element.text)

    def __handle_currency(self, element):
        name = element.text
        code = element.attrib['code']
        cid = element.attrib['id']
        self._user.set_currency(code, name)

    def parse(self, xmltext):
        root = etree.fromstring(xmltext)
        begin = date4(root.attrib['begin'])
        size = float(root.attrib['size'])
        self._envelope = Envelope(begin, size)
        for element in root:
            if self._factory.has_key(element.tag):
                handler = self._factory[element.tag]
                handler(element)
        return self._envelope

class TestUser(unittest.TestCase):

    def setUp(self):
        self.tree = etree.fromstring(USER_XML)

    def testCountry(self):
        element = self.tree.find("country")
        self.assertEqual(u"Россия", element.text)
        acode = element.attrib['code']
        self.assertEqual(u"ru", acode)

    def testCurrency(self):
        element = self.tree.find("currency")
        acode = element.attrib['code']
        aid = element.attrib['id']
        self.assertEqual(u"руб", element.text)
        self.assertEqual(u"RUB", acode)
        self.assertEqual(u"ru", aid)

    def testFirstDay(self):
        element = self.tree.find("firstDayOfWeek")
        self.assertEqual(u"2", element.text)

    def testTimeZone(self):
        element = self.tree.find("timeZone")
        self.assertEqual(u"Europe/Moscow", element.text)

    def testPersons(self):
        element = self.tree.find("persons")
        self.assertEqual(1, len(element))

        person = element.find('person')
        aid = person.attrib['id']
        self.assertEqual(u'15009487', aid)

        aname = person.attrib['name']
        self.assertEqual(u'Пользователь', aname)

    def testUser4k(self):
        user = User4k().parse(USER_XML)
        self.assertEqual(u"руб", user.currency_name)
        self.assertEqual(u"Россия", user.country_name)
        self.assertEqual(2, user.firstday)
        self.assertEqual(u'15009487', user.get_person_id())
        
class TestEnvelope(unittest.TestCase):

    def testEnvelope4k(self):
        e = Envelope4k().parse(ENVELOPE_XML)
        self.assertEqual(date4("2009-11-16"), e.begin)


if __name__ == "__main__":
	unittest.main()
